﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;

namespace MulticastTV
{

    /// <summary>
    /// Stores favourites to file before importing channel list and restores them after.
    /// </summary>
    class PreserveFavourites
    {
        string file;
        /// <summary>
        /// Constructor
        /// </summary>
        public PreserveFavourites(string commonAppPath) 
        {
            file = commonAppPath + @"\..\favtemp.xml";
        }

        public void saveBeforeImporting()
        { 
            //create new datatable from channelTable and save only favourites
            string fav_filter = ChannelTable.menu.Tables["Menu"].Columns[5].ColumnName.ToString() + "= true";
            DataView fav = new DataView(ChannelTable.menu.Tables["Menu"], fav_filter, ChannelTable.menu.Tables["Menu"].Columns[2].ColumnName, DataViewRowState.CurrentRows);
            fav.ToTable().WriteXml(file);
        }

        public void restoreAfterImporting()
        { 
           //read saved datatable, iterate and search for equal entries in channelTable.
            DataSet dsTemp = new DataSet();

            if (!File.Exists(file))
                return;
            dsTemp.ReadXml(file);

            if (dsTemp.Tables.Count > 0)
            {
                foreach (DataRow dr in dsTemp.Tables["Menu"].Rows)
                {
                    foreach (DataRow dr2 in ChannelTable.menu.Tables["Menu"].Rows)
                    {
                        if (dr["Name"].ToString() == dr2["Name"].ToString() && dr["Audio"].ToString() == dr2["Audio"].ToString())
                        {
                            dr2["Fav"] = true;
                        }
                    }
                }
            }
        }
    }
}
